//
//  RootViewController.m
//  one_mile_01
//
//  Created by 王雅蓉 on 15/8/21.
//  Copyright (c) 2015年 王雅蓉. All rights reserved.
//

#import "RootViewController.h"
#import "PilotTableViewCell.h"
//push到留学领航
#import "StudyPilotViewController.h"
#import "StudyPilotModel.h"
#import "tutorHomeViewController.h"
#import "ApplyjobsViewController.h"
#import "EntrepreneurialViewController.h"
#import "CampusViewController.h"
#import "ShareViewController.h"

//搜索
#import "SearchTutorViewController.h"

#import "UIImage+UIImageByScaleToSize.h"

#define Top_Height 172 * FITRATE
#define Center_Height 211 * FITRATE
#define Root_Width (WIDTH - 1 * FITRATE) / 2.0
#define Icon_Width 59 * FITRATE

@interface RootViewController ()

@end

@implementation RootViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        
        
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self createSubviews];
}

-(void)createSubviews
{
    UIView *navigationV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, NAVIGATIONHEIGHT)];
    navigationV.backgroundColor = BACKGROUND_MAIN_COLOR;
    [self.view addSubview:navigationV];
    
    UIView *searchV_main = [[UIView alloc] initWithFrame:CGRectMake(59 * FITRATE, NAVIGATIONHEIGHT - (9 + 27) * FITRATE, 128 * 2 * FITRATE, 27 * FITRATE)];
    searchV_main.backgroundColor = RGBACOLOR(30, 52, 74, 1.0);
    searchV_main.layer.masksToBounds = YES;
    searchV_main.layer.cornerRadius = 14.0f * FITRATE;
    [navigationV addSubview:searchV_main];
    
    UIImageView *searchIV = [[UIImageView alloc] initWithFrame:CGRectMake(20 * FITRATE, searchV_main.frame.size.height / 2.0 - 16 * FITRATE / 2.0, 20 * FITRATE, 16 * FITRATE)];
    searchIV.image = [UIImage imageNamed:@"root_search_icon@2x.png"];
    [searchV_main addSubview:searchIV];
    
    self.searchTF = [[UITextField alloc] initWithFrame:CGRectMake(searchIV.frame.origin.x + searchIV.frame.size.width + 10 * FITRATE, 0, searchV_main.frame.size.width - searchIV.frame.origin.x - searchIV.frame.size.width - 10 * FITRATE, searchV_main.frame.size.height)];
    self.searchTF.textColor = RGBACOLOR(142, 142, 142, 1.0);
    self.searchTF.font = [UIFont systemFontOfSize:15.0f];
    self.searchTF.text = @"搜索：学校/专业/导师";
    self.searchTF.clearsOnBeginEditing = YES;
    [searchV_main addSubview:_searchTF];
    
    self.searchTF.delegate = self;
    
    UITapGestureRecognizer *resign_rootGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(resignKeyboardAction:)];
    [self.view addGestureRecognizer:resign_rootGR];
    
    //第一行
    UIView *firstRootV = [[UIView alloc] initWithFrame:CGRectMake(0, NAVIGATIONHEIGHT, WIDTH, Top_Height)];
    firstRootV.backgroundColor = BACKGROUND_ASSISTANT_COLOR;
    [self.view addSubview:firstRootV];
    
    //求职就业
    UIButton *applyBT = [UIButton buttonWithType:UIButtonTypeCustom];
    applyBT.frame = CGRectMake(0, 0, Root_Width, Top_Height);
    applyBT.backgroundColor = [UIColor clearColor];
    applyBT.tag = 18000;
    [firstRootV addSubview:applyBT];
    
    UIImageView *applyIV = [[UIImageView alloc] initWithFrame:CGRectMake(applyBT.center.x - Icon_Width / 2.0, applyBT.center.y - Icon_Width / 2.0, Icon_Width, Icon_Width)];
    applyIV.image = [UIImage imageNamed:@"root_apply@2x.png"];
//    applyIV.backgroundColor = [UIColor yellowColor];
    [applyBT addSubview:applyIV];
    
    UILabel *applyL = [[UILabel alloc] initWithFrame:CGRectMake(applyBT.center.x - 80 * FITRATE, applyIV.frame.origin.y + applyIV.frame.size.height + 14 * FITRATE, 80 * FITRATE * 2, 18 * FITRATE)];
    applyL.backgroundColor = [UIColor clearColor];
    applyL.textAlignment = NSTextAlignmentCenter;
    applyL.text = @"求职就业";
    applyL.font = [UIFont systemFontOfSize:18.0 * FITRATE];
    applyL.font = [UIFont boldSystemFontOfSize:18.5 * FITRATE];
    applyL.textColor = BACKGROUND_MAIN_COLOR;
    [applyBT addSubview:applyL];
    
    //分割线
    UILabel *bL = [[UILabel alloc] initWithFrame:CGRectMake(Root_Width, 15 * FITRATE, 1 * FITRATE, Top_Height - 15 * FITRATE)];
    bL.backgroundColor = RGBACOLOR(221, 221, 221, 1.0);
    [firstRootV addSubview:bL];
    
    //创业助力
    UIButton *entreBT = [UIButton buttonWithType:UIButtonTypeCustom];
    entreBT.frame = CGRectMake(Root_Width + 1 * FITRATE, 0, Root_Width, Top_Height);
    entreBT.backgroundColor = [UIColor clearColor];
    entreBT.tag = 18001;
    [firstRootV addSubview:entreBT];
    
    UIImageView *entreIV = [[UIImageView alloc] initWithFrame:CGRectMake(applyIV.frame.origin.x, applyIV.frame.origin.y, Icon_Width, Icon_Width)];
    entreIV.image = [UIImage imageNamed:@"root_entre@2x.png"];
//    entreIV.backgroundColor = [UIColor yellowColor];
    [entreBT addSubview:entreIV];
    
    UILabel *entreL = [[UILabel alloc] initWithFrame:CGRectMake(applyL.frame.origin.x, applyL.frame.origin.y, applyL.frame.size.width, applyL.frame.size.height)];
    entreL.backgroundColor = [UIColor clearColor];
    entreL.textAlignment = NSTextAlignmentCenter;
    entreL.text = @"创业助力";
    entreL.font = [UIFont systemFontOfSize:18.0 * FITRATE];
    entreL.font = [UIFont boldSystemFontOfSize:18.5 * FITRATE];
    entreL.textColor = BACKGROUND_MAIN_COLOR;
    [entreBT addSubview:entreL];
    
    //第二行 留学领航
    UIButton *pilotBT = [UIButton buttonWithType:UIButtonTypeCustom];
    pilotBT.frame = CGRectMake(0, NAVIGATIONHEIGHT + Top_Height, WIDTH, Center_Height);
    pilotBT.tag = 18002;
    [pilotBT setBackgroundImage:[UIImage imageNamed:@"root_center_bg@2x.png"] forState:UIControlStateNormal];
    [self.view addSubview:pilotBT];
    
    UIImageView *pilotIV = [[UIImageView alloc] initWithFrame:CGRectMake(pilotBT.center.x - 76 * FITRATE / 2.0, 54 * FITRATE, 76 * FITRATE, 72 * FITRATE)];
    pilotIV.image = [UIImage imageNamed:@"root_pilot@2x.png"];
//    pilotIV.backgroundColor = [UIColor yellowColor];
    [pilotBT addSubview:pilotIV];
    
    UILabel *pilotL = [[UILabel alloc] initWithFrame:CGRectMake(pilotBT.center.x - 80 * FITRATE, pilotIV.frame.origin.y + pilotIV.frame.size.height + 14 * FITRATE, 80 * FITRATE * 2, 18 * FITRATE)];
    pilotL.text = @"留学领航";
    pilotL.textAlignment = NSTextAlignmentCenter;
    pilotL.textColor = [UIColor whiteColor];
    pilotL.font = [UIFont systemFontOfSize:18.0 * FITRATE];
    pilotL.font = [UIFont boldSystemFontOfSize:18.5 * FITRATE];
    pilotL.backgroundColor = [UIColor clearColor];
    [pilotBT addSubview:pilotL];
    
    //第三行
    UIView *secondView = [[UIView alloc] initWithFrame:CGRectMake(0, pilotBT.frame.origin.y + pilotBT.frame.size.height, WIDTH, Top_Height)];
    secondView.backgroundColor = BACKGROUND_ASSISTANT_COLOR;
    [self.view addSubview:secondView];
    
    //猎奇分享
    UIButton *shareBT = [UIButton buttonWithType:UIButtonTypeCustom];
    shareBT.frame = CGRectMake(0, 0, Root_Width, Top_Height);
    shareBT.backgroundColor = [UIColor clearColor];
    shareBT.tag = 18003;
    [secondView addSubview:shareBT];
    
    UIImageView *shareIV = [[UIImageView alloc] initWithFrame:CGRectMake(shareBT.center.x - Icon_Width / 2.0, shareBT.center.y - Icon_Width / 2.0, Icon_Width, Icon_Width)];
    shareIV.image = [UIImage imageNamed:@"root_share@2x.png"];
//    shareIV.backgroundColor = [UIColor yellowColor];
    [shareBT addSubview:shareIV];
    
    UILabel *shareL = [[UILabel alloc] initWithFrame:CGRectMake(shareBT.center.x - 80 * FITRATE, shareIV.frame.origin.y + shareIV.frame.size.height + 14 * FITRATE, 80 * FITRATE * 2, 18 * FITRATE)];
    shareL.backgroundColor = [UIColor clearColor];
    shareL.textAlignment = NSTextAlignmentCenter;
    shareL.text = @"猎奇分享";
    shareL.font = [UIFont systemFontOfSize:18.0 * FITRATE];
    shareL.font = [UIFont boldSystemFontOfSize:18.5 * FITRATE];
    shareL.textColor = BACKGROUND_MAIN_COLOR;
    [shareBT addSubview:shareL];
    
    //分割线
    UILabel *bL2 = [[UILabel alloc] initWithFrame:CGRectMake(Root_Width, 0, 1 * FITRATE, secondView.frame.size.height - 15 * FITRATE)];
    bL2.backgroundColor = RGBACOLOR(221, 221, 221, 1.0);
    [secondView addSubview:bL2];
    
    //校园生活
    UIButton *campusBT = [UIButton buttonWithType:UIButtonTypeCustom];
    campusBT.frame = CGRectMake(Root_Width + 1 * FITRATE, 0, Root_Width, Top_Height);
    campusBT.backgroundColor = [UIColor clearColor];
    campusBT.tag = 18004;
    [secondView addSubview:campusBT];
    
    UIImageView *campusIV = [[UIImageView alloc] initWithFrame:CGRectMake(shareIV.frame.origin.x, shareIV.frame.origin.y, Icon_Width, Icon_Width)];
    campusIV.image = [UIImage imageNamed:@"root_campus@2x.png"];
//    campusIV.backgroundColor = [UIColor yellowColor];
    [campusBT addSubview:campusIV];
    
    UILabel *campusL = [[UILabel alloc] initWithFrame:CGRectMake(shareL.frame.origin.x, shareL.frame.origin.y, shareL.frame.size.width, shareL.frame.size.height)];
    campusL.backgroundColor = [UIColor clearColor];
    campusL.textAlignment = NSTextAlignmentCenter;
    campusL.text = @"校园生活";
    campusL.font = [UIFont systemFontOfSize:18.0 * FITRATE];
    campusL.font = [UIFont boldSystemFontOfSize:18.5 * FITRATE];
    campusL.textColor = BACKGROUND_MAIN_COLOR;
    [campusBT addSubview:campusL];
    
    [applyBT addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [entreBT addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [pilotBT addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [shareBT addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    [campusBT addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark -- buttonAction
-(void)buttonAction:(UIButton *)button
{
    if (button.tag == 18000) {
        
        ApplyjobsViewController *applyjobsVC = [[ApplyjobsViewController alloc] init];
        [self.navigationController pushViewController:applyjobsVC animated:YES];
    } else if (button.tag == 18001) {
    
        EntrepreneurialViewController *entreVC = [[EntrepreneurialViewController alloc] init];
        [self.navigationController pushViewController:entreVC animated:YES];
    } else if (button.tag == 18002) {
    
        StudyPilotViewController *pilotVC = [[StudyPilotViewController alloc] init];
        [self.navigationController pushViewController:pilotVC animated:YES];
    } else if (button.tag == 18003) {
    
        ShareViewController *shareVC = [[ShareViewController alloc] init];
        [self.navigationController pushViewController:shareVC animated:YES];
    } else {
    
        CampusViewController *campusVC = [[CampusViewController alloc] init];
        [self.navigationController pushViewController:campusVC animated:YES];
    }
    self.searchTF.text = @"搜索：学校/专业/导师";
    [self.searchTF resignFirstResponder];
}

//取消第一响应
-(void)resignKeyboardAction:(UITapGestureRecognizer *)tap
{
    self.searchTF.text = @"搜索：学校/专业/导师";
    [self.view endEditing:YES];
}

#pragma mark -- textfieldDelegate
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    self.searchTF.text = @"";
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    if (textField.text.length == 0) {
        
        self.searchTF.text = @"搜索：学校/专业/导师";
    } else {
    
        SearchTutorViewController *searchVC = [[SearchTutorViewController alloc] init];
        searchVC.searchKey = textField.text;
        searchVC.stateForSearch = 1;
        
        [self.navigationController pushViewController:searchVC animated:YES];
    }
    [textField resignFirstResponder];
    return NO;
}

#pragma mark -- viewWillAppear & viewWillDisappear
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBar.alpha = 0;
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.tabBarController.tabBar.hidden = NO;
}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
//    self.navigationController.navigationBar.alpha = 1;
    [self.navigationController setNavigationBarHidden:NO animated:NO];
    self.tabBarController.tabBar.hidden = NO;
}

-(UIStatusBarStyle)preferredStatusBarStyle
{
    return UIStatusBarStyleDefault;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
